package vsvm.gui.forms;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import vsvm.data.filter.AbstractFilter;
import vsvm.data.model.AbstractDataModel;
import vsvm.gui.Environment;

public class SelectFilterDialog extends JDialog {
	private JPanel jContentPane = null;
	private JPanel buttonsPanel = null;
	private JButton btnOk = null;
	private JButton btnCancel = null;
	private JPanel centerPanel = null;
	private JLabel lblName = null;
	private JTextField txtName = null;
	private JLabel lblType = null;
	private JComboBox cmbTypes = null;
	
	private AbstractDataModel dm;
	
	private class DataSourceCreateAction extends AbstractAction {
		private SelectFilterDialog dlg;
		
		public DataSourceCreateAction(SelectFilterDialog d) {
			dlg = d;			
		}

		public void actionPerformed(ActionEvent e) {
			dlg.filter();			
		}
	}
	
	private class DataSourceCancelAction extends AbstractAction {
		private SelectFilterDialog dlg;
		
		public DataSourceCancelAction(SelectFilterDialog d) {
			dlg = d;			
		}

		public void actionPerformed(ActionEvent e) {
			dlg.dispose();			
		}
	}
	
	
	
	public void filter() {
		AbstractFilter s = (AbstractFilter)cmbTypes.getSelectedItem();
		if (s == null) return;	
		
		FilterForm ff = new FilterForm(Environment.getEnvironment().getMainFrame(), s);//new FilterForm(Environment.getEnvironment().getMainFrame(), s);
		ff.setDataModel(dm);
		ff.setVisible(true);
		
		this.dispose();
	}

	/**
	 * This method initializes buttonsPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getButtonsPanel() {
		if (buttonsPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(25);
			buttonsPanel = new JPanel();
			buttonsPanel.setLayout(flowLayout);
			buttonsPanel.setPreferredSize(new java.awt.Dimension(10,40));
			buttonsPanel.add(getBtnCreate(), null);
			buttonsPanel.add(getBtnCancel(), null);
		}
		return buttonsPanel;
	}

	/**
	 * This method initializes btnCreate	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCreate() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setPreferredSize(new java.awt.Dimension(80,25));
			btnOk.setAction(new DataSourceCreateAction(this));
			btnOk.setText("OK");
		}
		return btnOk;
	}

	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setPreferredSize(new java.awt.Dimension(80,25));
			btnCancel.setAction(new DataSourceCancelAction(this));
			btnCancel.setText("Cancel");
		}
		return btnCancel;
	}

	/**
	 * This method initializes centerPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getCenterPanel() {
		if (centerPanel == null) {
			lblType = new JLabel();
			lblType.setText("Filter");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new java.awt.Insets(5,10,5,15);
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new java.awt.Insets(5,15,5,10);
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.gridy = 0;
			centerPanel = new JPanel();
			centerPanel.setLayout(new GridBagLayout());
			centerPanel.add(lblType, gridBagConstraints);
			centerPanel.add(getCmbTypes(), gridBagConstraints1);
		}
		return centerPanel;
	}

	/**
	 * This method initializes cmbTypes	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCmbTypes() {
		if (cmbTypes == null) {
			cmbTypes = new JComboBox();
			loadSources();
		}
		return cmbTypes;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	}

	/**
	 * This is the default constructor
	 */
	public SelectFilterDialog(JFrame parent, AbstractDataModel m) {
		super(parent);
		setLocationRelativeTo(parent);
		initialize();
		dm = m;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(423, 173);
		this.setTitle("Select filter");
		this.setResizable(false);
		this.setPreferredSize(new java.awt.Dimension(180,120));
		this.setName("CreateDataSourceDialog");
		this.setModal(true);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
		getRootPane().setDefaultButton(btnOk);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getButtonsPanel(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getCenterPanel(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}
	
	private void loadSources() {
		cmbTypes.setModel(new DefaultComboBoxModel(Environment.getEnvironment().getWorkspace().getFilters().toArray()));		
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
